<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Select a server matching a read preference</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongodb-driver-manager.removesubscriber.html">« MongoDB\Driver\Manager::removeSubscriber</a></li>
      <li style="float: right;"><a href="mongodb-driver-manager.startsession.html">MongoDB\Driver\Manager::startSession »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mongodb-driver-manager.html">MongoDB\Driver\Manager</a></li>
    <li>Select a server matching a read preference</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb-driver-manager.selectserver" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\Driver\Manager::selectServer</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\Driver\Manager::selectServer</span> &mdash; <span class="dc-title">Select a server matching a read preference</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-driver-manager.selectserver-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><strong>MongoDB\Driver\Manager::selectServer</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="class.mongodb-driver-readpreference.html" class="type MongoDB\Driver\ReadPreference">MongoDB\Driver\ReadPreference</a></span><span class="type"></span></span> <code class="parameter">$readPreference</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-server.html" class="type MongoDB\Driver\Server">MongoDB\Driver\Server</a></span></div>

  <p class="para rdfs-comment">
   Selects a <span class="classname"><a href="class.mongodb-driver-server.html" class="classname">MongoDB\Driver\Server</a></span> matching
   <code class="parameter">readPreference</code>. If
   <code class="parameter">readPreference</code> is <strong><code>null</code></strong> or omitted, the primary
   server will be selected by default. This may be used to preselect a server in
   order to perform version checking before executing an operation.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    Unlike <span class="function"><a href="mongodb-driver-manager.getservers.html" class="function">MongoDB\Driver\Manager::getServers()</a></span>, this method
    will initialize database connections and perform server discovery if
    necessary. See the
    <a href="https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#single-threaded-server-selection" class="link external">&raquo;&nbsp;Server Selection Specification</a>
    for additional information.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-driver-manager.selectserver-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">readPreference</code> (<span class="classname"><a href="class.mongodb-driver-readpreference.html" class="classname">MongoDB\Driver\ReadPreference</a></span>)</dt>

    <dd>

     <p class="para">
      The read preference to use for selecting a server. If <strong><code>null</code></strong> or omitted,
      the primary server will be selected by default.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb-driver-manager.selectserver-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a <span class="classname"><a href="class.mongodb-driver-server.html" class="classname">MongoDB\Driver\Server</a></span> matching the read
   preference.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongodb-driver-manager.selectserver-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="simplelist">
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> on argument parsing errors.</li><li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-connectionexception.html" class="classname">MongoDB\Driver\Exception\ConnectionException</a></span> if connection to the server fails (for reasons other than authentication).</li><li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-authenticationexception.html" class="classname">MongoDB\Driver\Exception\AuthenticationException</a></span> if authentication is needed and fails.</li>
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-runtimeexception.html" class="classname">MongoDB\Driver\Exception\RuntimeException</a></span> if a server matching the read preference could not be found.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-manager.selectserver-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL mongodb 1.11.0</td>
       <td>
        The <code class="parameter">readPreference</code> is now optional. If <strong><code>null</code></strong> or
        omitted, the primary server will be selected by default.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongodb-driver-manager.selectserver-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="classname"><a href="class.mongodb-driver-server.html" class="classname">MongoDB\Driver\Server</a></span></li>
   <li class="member"><span class="function"><a href="mongodb-driver-manager.getservers.html" class="function" rel="rdfs-seeAlso">MongoDB\Driver\Manager::getServers()</a> - Return the servers to which this manager is connected</span></li>
   <li class="member"><a href="https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst" class="link external">&raquo;&nbsp;Server Selection Specification</a></li>
  </ul>
 </div>


</div></div></div></body></html>